/**
 * Created by 77element-01 on 2016/2/2.
 */
function bodyResize() {
    $("body").css("max-height", $(window).height());
    $(".demo").css("height", $(window).height() - 64);
    $("#content-md").css("height", $(window).height() - 44);
    $('.viewArea').css('height', $(window).height() - 74);
    $('.viewArea-c').css('height', $(window).height() - 100);

    //$('.viewArea').css('min-height',$(window).height() - 74);
    //$('.viewArea-c').height($(window).height() - 100);

    if ($(window).width() < 980) {
        $('body').removeClass('edit').addClass('devpreview');
        $('#devpreview1').find('i').removeClass('icon-angle-left').addClass('icon-angle-right');
    } else {
        $('body').removeClass('devpreview').addClass('edit');
        $('#devpreview1').find('i').removeClass('icon-angle-right').addClass('icon-angle-left');
    }
}

function toolbars() {
    $('.navbar-inverse').hover(function () {
        $(this).stop(true).animate({height: '80'}, 200).find('.toolbars').show();
        $('body').stop(true).animate({paddingTop: '80'}, 200);
    }, function () {
        $(this).stop(true).animate({height: '50'}, 200).find('.toolbars').hide();
        $('body').stop(true).animate({paddingTop: '50'}, 200);
        $(this).find('li.dropdown').removeClass('open');
    })
}

function theme() {
    $('.theme li').click(function (e) {
        e.preventDefault();
        var style = $(this).attr("id");
        var $this = $(this).siblings();
        $("link[title='" + style + "']").removeAttr("disabled");
        $.each($this, function () {
            $("link[title='" + $(this).attr('id') + "']").attr("disabled", "disabled");
        })
        $.cookie("mystyle", style, {expires: 30});
    })

    var cookie_style = $.cookie("mystyle");
    if (cookie_style == null) {
        $("link[title='blue']").removeAttr("disabled");
    } else {
        $.each($('.theme li'), function () {
            $("link[title='" + $(this).attr('id') + "']").attr("disabled", "disabled");
        })
        $("link[title='" + cookie_style + "']").removeAttr("disabled");
    }
}

function leftSidebar() {
    $("#devpreview1").click(function (e) {
        e.preventDefault();
        $("body").removeClass("sourcepreview");
        $("body").toggleClass("edit");
        $("body").toggleClass("devpreview");
        $(this).next().toggleClass('fa-angle-left');
        $(this).find('i').toggleClass('fa-angle-right');
        $(this).addClass("active");
        return false
    });
}

function scrollbar() {
    $.mCustomScrollbar.defaults.scrollButtons.enable = true; //enable scrolling buttons by default
    $.mCustomScrollbar.defaults.axis = "y";
    $("#content-md").mCustomScrollbar({theme: "minimal-dark"});
    $(".viewArea-c").mCustomScrollbar({theme: "minimal-dark"});
}

//input纯数字
function ipPort(obj) {
    $(obj).on({
        'focus': function (e) {
            $(this).val().replace(/[^0-9]/g, "");
        },
        'keypress': function (e) {
            var char = String.fromCharCode(e.keyCode)
            var re = /[0-9]/g
            e.returnValue = char.match(re) != null ? true : false
        },
        'keyup': function (e) {
            if (e.type.indexOf("key") != -1) {
                var re = /37|38|39|40/g
                if (e.keyCode.toString().match(re)) return false
            }
            e.target.value = e.target.value.replace(/[^0-9]/g, "");
            if (e.keyCode == 110 || e.keyCode == 190) {
                $(this).next().focus();
            }
        }
    })
}

function navelement() {
    return '<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">' +
        '<div class="navbar-header">' +
        '<a class="navbar-brand brand" href="#"><img src="image/favicon.ico"> 综合管理平台</a>' +
        '</div>' +
        '<div>' +
        '<ul class="nav navbar-nav fl dn">' +
        '<li title="管理" class="contrast">' +
        '<a href="#"><i class="i-2x i-716"></i></a>' +
        '</li>' +
        '<li title="添加" class="addServer">' +
        '<a href="#"><i class="i-2x i-739"></i></a>' +
        '</li>' +
        '<li title="对比">' +
        '<a href="#"><i class="i-2x i-708"></i></a>' +
        '</li>' +
        '</ul>' +
        '<ul class="nav navbar-nav fr">' +
        '<!-- 用户登录信息开始 -->' +
        '<li class="dropdown user">' +
        '<a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown">' +
        '<span class="username"><i class="i-757"></i>&nbsp;&nbsp;Admin</span>' +
        '<i class="fa fa-angle-down"></i>' +
        '</a>' +
        '<ul class="dropdown-menu">' +
        '<li><a href="extra_profile.html"><i class="fa fa-user"></i> 我的资料</a></li>' +
        '<li><a href="page_calendar.html"><i class="fa fa-calendar"></i> 我的日历</a></li>' +
        '<li class="divider"></li>' +
        '<li><a href="extra_lock.html"><i class="fa fa-lock"></i> 锁定屏幕</a></li>' +
        '<li><a href="login.html"><i class="fa fa-key"></i> 注销</a></li>' +
        '</ul>' +

        '</li>' +
        '<!-- 用户登录信息结束 -->' +
        '</ul>' +
        '</div>' +
        '<!--功能条-->' +
        '<div class="toolbars cb">' +
        '<div class="template">' +
        '<div class="refresh" title="刷新">' +
        '<i class="fa fa-refresh"></i>' +
        '</div>' +
        '<div class="t-size" title="缩大/小">' +
        '<i class="fa fa-th-large"></i>' +
        '</div>' +
        '<div id="clear" class="t-clear" title="清空视域">' +
        '<i class="fa fa-trash"></i>' +
        '</div>' +
        '</div>' +
        '<div class="compare" title="比较">' +
        '<img src="./image/compare.png" alt="">' +
        '</div>' +
        '<div class="viewHead">' +
        '<div class="crowd">吞吐量(流入和流出)</div>' +
        '<div class="begin">' +
        '<input type="text" name="begin" class="dtpicker">' +
        '</div>' +
        '<div class="line">—</div>' +
        '<div class="end">' +
        '<input type="text" name="end" class="dtpicker">' +
        '</div>' +
        '<div class="lastHour">最近一小时</div>' +
        '<div class="submit">' +
        '<i class="fa fa-check-square-o"></i>' +
        '<input type="hidden" name="allbegin">' +
        '<input type="hidden" name="allend">' +
        '</div>' +
        '</div>' +
        '</div>' +
        '</nav>';
}

$(function () {
    $('body').prepend(navelement());
    scrollbar();//滚动条
    bodyResize();//自适应框架
    toolbars();//功能条
    theme();//主题切换
    leftSidebar();//左侧边栏收缩

    //$('.content-m').css('min-height', $(window).height() - 100);
    $(window).resize(function () {
        bodyResize();
    })
    $('.dropdown-toggle').dropdown();
    $('.refresh').click(function () {
        window.location.reload();//页面刷新
    })

    $(".nav-header").click(function () {
        var $par = $(this).parent(),
            $nex = $(this).next();
        $(".sidebar-nav > ul.nav-list").not($par).removeClass('bg-color');
        $par.toggleClass('bg-color');
        $(".sidebar-nav .boxes, .sidebar-nav .rows").not($nex).hide(200);
        $nex.toggle(200);
    });


})